package acm.蓝桥16;

import java.util.*;

public class D旅费报销 {
    /**
     *选出日期任意两张=>k，且总价值是最靠近<=m的票据
     * 分析：相当于，一堆二维点
     * 选出这些二维点x的差距>=k，且这些二位点的sum[y]最接近m
     * 暴力：
     *      枚举每个初始点
     *      解锁该初始点的所有>k的点
     *      再枚举所有
     */
    static int n,m,k;
    static int maxN=1000,maxM=5000;

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        k=sc.nextInt();
        List<int[]> list=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int[] ints = new int[2];
            ints[0] = day(sc.nextInt(), sc.nextInt());
            ints[1]=sc.nextInt();
            list.add(ints);
        }
        list.sort((a,b)->a[0]-b[0]);
        int dfs = dfs(list, 0);
        System.out.println(dfs);
    }

    static final int[] mm={0,31,28,31,30,31,30,31,31,30,31,30,31};


    public static int day(int m,int d){
        int day=0;
        for (int i = 0; i < m; i++) {
            day+=mm[i];
        }
        return day+d;
    }

    public static int dfs(List<int[]> list,int w){
        if (w>m) return 0;
        if (list.size()==0) return w;
        int size = list.size();
        int res=w;
        for (int i = 0; i < size; i++) {
            List<int[]> arg=new ArrayList<>();
            int j=0;
            int[] left = list.get(j++);
            int[] cur = list.get(i);
            while (cur[0]-left[0]>=k){
                arg.add(left);
                left=list.get(j++);
            }
            j=size-1;
            int[] right=list.get(j--);
            int[][] stack=new int[size][];
            int top=-1;
            while (right[0]-cur[0]>=k){
                stack[++top]=right;
                right=list.get(j--);
            }
            while (top!=-1){
                arg.add(stack[top--]);
            }
            int dfs = dfs(arg, w + cur[1]);
            res=Math.max(dfs,res);
        }
        return res;
    }
}
